class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (digits.size() == 0) {
            return vector<int>();
        }
        reverse(digits.begin(), digits.end());

        int carry = 1;
        for (int i = 0; carry || i < digits.size(); ++i) {
            if (i < digits.size()) {
                carry += digits[i];
            }
            if (i >= digits.size()) {
                digits.push_back(carry % 10);
            }
            else {
                digits[i] = carry % 10;
            }
            carry /= 10;
         }
         reverse(digits.begin(), digits.end());
         return digits;
    }
};